package io.renren.modules.peoplemanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.peoplemanage.dao.SysClassDao;
import io.renren.modules.peoplemanage.entity.SysClassEntity;
import io.renren.modules.peoplemanage.service.SysClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.*;
import java.util.function.Function;

/**
 *
 *
 * @author kiven
 * @email 2213593288@qq.com
 * @date 2020-09-04 16:50:44
 */
@Service("sysClassService")
public class SysClassServiceImpl extends ServiceImpl<SysClassDao, SysClassEntity> implements SysClassService {
    @Autowired
    SysClassDao sysClassDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysClassEntity> page = this.page(
                new Query<SysClassEntity>().getPage(params),
                new QueryWrapper<SysClassEntity>().select().orderByDesc("create_time").eq("is_delete",0).eq("is_graduate",0)
        );
        return new PageUtils(page);
    }

    @Override
    public void removeByIdAndUpdateNames(List<String> idsAndNames) {
        Iterator<String> iterator = idsAndNames.iterator();
        while (iterator.hasNext()){
            String next = iterator.next();
            String[] split = next.split("-");
            Integer id = Integer.parseInt(split[0]);
            String className = split[1];
            SysClassEntity sysClassEntity = new SysClassEntity();
            sysClassEntity.setClassId(id);
            sysClassEntity.setIsDelete(1);
            sysClassEntity.setClassName(className+"[已删除]"+System.currentTimeMillis());
            sysClassEntity.setUpdateTime(new Date());
            sysClassDao.updateById(sysClassEntity);
        }
    }

    @Transactional
    @Override
    public void topUpdateById(Map<String, Object> sysClass) {
        // TODU 旧班级是否存在, 新班级是否存在,
        Integer classId = (Integer)sysClass.get("classId");
        String oldClassName = String.valueOf(sysClass.get("oldClassName"));
        String newClassName = String.valueOf(sysClass.get("newClassName"));
        SysClassEntity sysClassEntity = new SysClassEntity();
        sysClassEntity.setClassId(classId);
        sysClassEntity.setClassName(newClassName);
        SysClassEntity sysClassEntity1 = sysClassDao.selectById(classId);
        if(!sysClassEntity1.getClassName().equals(oldClassName)){ //添加了事务 ,防止班级名称并发被修改
            throw new RuntimeException("原班级名称有误,请重新填写");
        }
        try {
            sysClassDao.updateById(sysClassEntity);
        }catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("目标班级名称已存在");
        }
    }

    @Override
    public void classGraduate(int id) {
        SysClassEntity entity = new SysClassEntity();
        entity.setClassId(id);
        entity.setIsGraduate(1);
        sysClassDao.updateById(entity);
    }

    @Override
    public List<String> findClassNames() {
        return sysClassDao.selectClassNames();
    }

    @Override
    public boolean save(SysClassEntity entity) {
        QueryWrapper<SysClassEntity> wrapper = new QueryWrapper<SysClassEntity>();
        wrapper.eq("class_name",entity.getClassName()).eq("is_delete",0);
        List<SysClassEntity> sysClassEntities = sysClassDao.selectList(wrapper);
        if(sysClassEntities.size() >=1){
            throw new RuntimeException("班级名称已存在");
        }
        if(entity.getClassName() == null){
            throw new RuntimeException("班级名称不能为空");
        }
        if(entity.getClassName().length()>8){
            throw new RuntimeException("班级名称过长");
        }
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        int insert = sysClassDao.insert(entity);
        return true;
    }

    @Override
    public boolean saveBatch(Collection<SysClassEntity> entityList) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<SysClassEntity> entityList) {
        return false;
    }

    @Override
    public boolean removeById(Serializable id) {
        return false;
    }

    @Override
    public boolean removeByMap(Map<String, Object> columnMap) {
        return false;
    }

    @Override
    public boolean remove(Wrapper<SysClassEntity> queryWrapper) {
        return false;
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        if(StringUtils.isEmpty(idList)){
            throw new RuntimeException("请选择");
        }
        SysClassEntity sysClassEntity = new SysClassEntity();
        Iterator<? extends Serializable> iterator = idList.iterator();
        while(iterator.hasNext()){
            Serializable next = iterator.next();
            Integer id =  (Integer)next;
            sysClassEntity.setClassId(id);
            sysClassEntity.setIsDelete(1);
            sysClassDao.updateById(sysClassEntity);
        }
        return true;
    }

    @Override
    public boolean updateById(SysClassEntity entity) {
        UpdateWrapper<SysClassEntity> updateWrapper = new UpdateWrapper<SysClassEntity>();
        updateWrapper.eq("class_id",entity.getClassId());
        sysClassDao.update(entity, updateWrapper);
        return true;
    }

    @Override
    public boolean update(Wrapper<SysClassEntity> updateWrapper) {
        return false;
    }

    @Override
    public boolean update(SysClassEntity entity, Wrapper<SysClassEntity> updateWrapper) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<SysClassEntity> entityList) {
        return false;
    }

    @Override
    public SysClassEntity getById(Serializable id) {
        return sysClassDao.selectById(id);
    }

    @Override
    public List<SysClassEntity> listByIds(Collection<? extends Serializable> idList) {
        return null;
    }

    @Override
    public List<SysClassEntity> listByMap(Map<String, Object> columnMap) {
        return null;
    }

    @Override
    public SysClassEntity getOne(Wrapper<SysClassEntity> queryWrapper) {
        return null;
    }

    @Override
    public int count() {
        return 0;
    }

    @Override
    public int count(Wrapper<SysClassEntity> queryWrapper) {
        return 0;
    }

    @Override
    public List<SysClassEntity> list(Wrapper<SysClassEntity> queryWrapper) {
        return null;
    }

    @Override
    public List<SysClassEntity> list() {
        return null;
    }

    @Override
    public <E extends IPage<SysClassEntity>> E page(E page, Wrapper<SysClassEntity> queryWrapper) {
        return this.getBaseMapper().selectPage(page, queryWrapper);
    }

    @Override
    public <E extends IPage<SysClassEntity>> E page(E page) {
        return null;
    }

    @Override
    public List<Map<String, Object>> listMaps(Wrapper<SysClassEntity> queryWrapper) {
        return null;
    }

    @Override
    public List<Map<String, Object>> listMaps() {
        return null;
    }

    @Override
    public List<Object> listObjs() {
        return null;
    }

    @Override
    public <V> List<V> listObjs(Function<? super Object, V> mapper) {
        return null;
    }

    @Override
    public List<Object> listObjs(Wrapper<SysClassEntity> queryWrapper) {
        return null;
    }

    @Override
    public <V> List<V> listObjs(Wrapper<SysClassEntity> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    @Override
    public <E extends IPage<Map<String, Object>>> E pageMaps(E page, Wrapper<SysClassEntity> queryWrapper) {
        return null;
    }

    @Override
    public <E extends IPage<Map<String, Object>>> E pageMaps(E page) {
        return null;
    }

    @Override
    public QueryChainWrapper<SysClassEntity> query() {
        return null;
    }

    @Override
    public LambdaQueryChainWrapper<SysClassEntity> lambdaQuery() {
        return null;
    }

    @Override
    public UpdateChainWrapper<SysClassEntity> update() {
        return null;
    }

    @Override
    public LambdaUpdateChainWrapper<SysClassEntity> lambdaUpdate() {
        return null;
    }

    @Override
    public boolean saveOrUpdate(SysClassEntity entity, Wrapper<SysClassEntity> updateWrapper) {
        return false;
    }
}